package com.example.springboot_06_customglobalexception.controller;

import com.example.springboot_06_customglobalexception.common.CustomException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(path = "page")
public class ExceptionController {
    @ResponseBody
    @GetMapping (path = "hello")
    public String hello(){
        return "hello";
    }

    /*
    抛出自定义异常
     */
    @ResponseBody
    @GetMapping(path = "divide")
    public int divide(int sub) {
        try {
            return 1000 / sub;
        } catch (Exception e) {
            throw new CustomException("除0异常！");
        }
    }

    private final int[] ans = new int[]{1, 2, 3, 4};

    @ResponseBody
    @GetMapping(path = "ary")
    public int ary(int index) {
        return ans[index];
    }
}

